package com.xtaller.easy.admin.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.J;
import com.xtaller.common.convert.V;
import com.xtaller.easy.admin.base.TApi;
import com.xtaller.easy.admin.config.R;
import com.xtaller.easy.admin.model.SysDict;
import com.xtaller.easy.admin.service.impl.SysDictService;
import com.xtaller.easy.admin.specification.DictCreate;
import com.xtaller.easy.admin.specification.DictModuleCreate;
import com.xtaller.easy.admin.specification.DictModuleUpdate;
import com.xtaller.easy.admin.specification.DictUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Taller on 2018/4/14
 */
@Api(tags = "数据字典api")
@RestController
@RequestMapping("/v1/sys")
@CrossOrigin   //跨域服务注解
public class DictApi extends TApi{
    @Autowired
    private SysDictService dictService;

    private Map<String,String> DictModuleVerify(){
        Map<String,String> verify = new HashMap<>();
        verify.put("name","模块名称不能为空");
        verify.put("code","模块编码不能为空");
        return verify;
    }
    private Map<String,String> DictVerify(){
        Map<String,String> verify = new HashMap<>();
        verify.put("moduleId","模块Id不能为空");
        verify.put("parentId","字典父级Id不能为空");
        verify.put("name","字典名称不能为空");
        verify.put("code","字典编码不能为空");
        return verify;
    }
    /***********************字典模块************************/
    @GetMapping("/dict-module")
    @ApiOperation(value = "读取字典模块")
    public Object getModule(){
        List<SysDict> dicts = dictService.getModule();
        return R.ok(data2Grid(dicts.size(), dicts));
    }

    @PostMapping("/dict-module")
    @ApiOperation(value = "创建字典模块")
    public Object createModule(@RequestBody DictModuleCreate object){
        JSONObject check = V.checkEmpty(DictModuleVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysDict dict = J.o2m(object, SysDict.class);
        dict.setCreator(getUserIdByToken());
        // 模块唯一性判断
        if(dictService.existModule(dict, true))
            return R.error("模块名称已经存在请更换");
        // 编码唯一性判断
        if(dictService.existCode(dict, true))
            return R.error("模块编码已经存在请更换");
        dict = dictService.createSysDict(dict);
        if(dict == null)
            return R.error("创建模块失败,请稍后再试");
        return R.ok("创建模块成功", formatObject(dict));
    }

    @PutMapping("/dict-module")
    @ApiOperation(value = "修改字典模块")
    public Object updateModule(@RequestBody DictModuleUpdate object){
        if(object.getId() == null)
            return R.error("数据Id不能为空");
        if(dictService.selectById(object.getId()) == null)
            return R.error("数据Id异常");
        JSONObject check = V.checkEmpty(DictModuleVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysDict dict = J.o2m(object, SysDict.class);
        dict.setReviser(getUserIdByToken());
        // 模块唯一性判断
        if(dictService.existModule(dict, false))
            return R.error("模块名称已经存在请更换");
        // 编码唯一性判断
        if(dictService.existCode(dict, false))
            return R.error("模块编码已经存在请更换");
        dict = dictService.updateSysDict(dict);
        if(dict == null)
            return R.error("修改模块失败,请稍后再试");
        return R.ok("修改模块成功", formatObject(dict));
    }

    @DeleteMapping("/dict-module/{id}")
    @ApiOperation(value = "删除字典模块")
    public Object deleteModule(@PathVariable("id") String id){
        if(dictService.selectById(id) == null)
            return R.error("数据Id异常");
        List<SysDict> list = dictService.getByModuleIdOrParentId(id);
        if(list.size() > 0)
            return R.error("该模块下有子集数据，禁止删除！");
        if(dictService.deleteSysDict(id, getUserIdByToken()))
            return R.ok("字典模块删除成功");
        return R.error("删除失败请稍后再试");
    }

    /************************字典*************************/
    @GetMapping("/dict/{moduleId}")
    @ApiOperation(value = "读取字典")
    public Object getDictByModuleId(@PathVariable("moduleId") Long moduleId){
        List<SysDict> dicts = dictService.getByModuleId(moduleId);
        return R.ok(data2Grid(dicts.size(), dicts));
    }

    @PostMapping("/dict")
    @ApiOperation(value = "创建字典")
    public Object createDict(@RequestBody DictCreate object){
        JSONObject check = V.checkEmpty(DictVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysDict dict = J.o2m(object, SysDict.class);
        dict.setCreator(getUserIdByToken());
        // 模块唯一性判断
        if(dictService.existName(dict, true))
            return R.error("字典名称已经存在请更换");
        // 编码唯一性判断
        if(dictService.existCode(dict, true))
            return R.error("字典编码已经存在请更换");
        dict = dictService.createSysDict(dict);
        if(dict == null)
            return R.error("创建字典失败,请稍后再试");
        return R.ok("创建字典成功", formatObject(dict));
    }

    @PutMapping("/dict")
    @ApiOperation(value = "修改字典")
    public Object updateDict(@RequestBody DictUpdate object){
        if(object.getId() == null)
            return R.error("数据Id不能为空");
        if(dictService.selectById(object.getId()) == null)
            return R.error("数据Id异常");
        JSONObject check = V.checkEmpty(DictVerify(), object);
        if(check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysDict dict = J.o2m(object, SysDict.class);
        dict.setReviser(getUserIdByToken());
        // 模块唯一性判断
        if(dictService.existName(dict, false))
            return R.error("字典名称已经存在请更换");
        // 编码唯一性判断
        if(dictService.existCode(dict, false))
            return R.error("字典编码已经存在请更换");
        dict = dictService.updateSysDict(dict);
        if(dict == null)
            return R.error("修改字典失败,请稍后再试");
        return R.ok("修改字典成功", formatObject(dict));
    }

    @DeleteMapping("/dict/{id}")
    @ApiOperation(value = "删除字典")
    public Object deleteDict(@PathVariable("id") String id){
        if(dictService.selectById(id) == null)
            return R.error("数据Id异常");
        List<SysDict> list = dictService.getByModuleIdOrParentId(id);
        if(list.size() > 0)
            return R.error("该字典下有子集数据，禁止删除！");
        if(dictService.deleteSysDict(id, getUserIdByToken()))
            return R.ok("字典删除成功");
        return R.error("删除失败请稍后再试");
    }


    @GetMapping("/dict-global")
    @ApiOperation(value = "读取所有的字典数据")
    public Object getAllDict(){
        return R.ok(dictService.getAllDict());
    }

    @GetMapping("/dict-by-code")
    @ApiOperation(value = "读取相应code下的字典数据")
    public Object getDictByCode(){
        return R.ok(dictService.getDictByCode());
    }

}
